package com.yuanfengerp.Announcement.service.impl;

import com.yuanfengerp.Announcement.dao.IAnnouncementDao;
import com.yuanfengerp.Announcement.model.Announcement;
import com.yuanfengerp.Announcement.service.IAnnouncementService;
import com.yuanfengerp.publicUtil.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class AnnouncementServiceImpl implements IAnnouncementService {
@Autowired
private IAnnouncementDao announcementDao;
    @Override
    public ArrayList<Announcement> getAnnouncementList(int pageSize, int offset) {
        return announcementDao.getAnnouncementList(pageSize,offset);
    }

    @Override
    public ArrayList<Announcement> getAnnouncement(String caption, Date creation_time, Integer status , int notification_id) {
        return announcementDao.getAnnouncement(caption,creation_time,status, notification_id);
    }

    @Override
    public Result<Announcement> addAnnouncement(Announcement announcement) {
//        if(announcement.getContent() == null)
//            return Result.failure(400,"请输入内容");
//        if(announcement.getCaption() == null)
//            return Result.failure(400,"请输入标题");
        announcement.setCreation_time(new Date());
        announcement.setPublish("否");
        announcement.setRead_count("0");
        announcement.setUnread("0");
        announcement.setCreator("admin");
        int i = announcementDao.addAnnouncement(announcement);
        return i > 0 ? Result.success(announcement) : Result.failure(400,"添加失败");
    }

    @Override
    public Result<Announcement> publishAnnouncement(Announcement announcement) {
        if(announcement.getContent() == null)
            return Result.failure(400,"请输入内容");
        if(announcement.getCaption() == null)
            return Result.failure(400,"请输入标题");
        announcement.setCreation_time(new Date());
        announcement.setPublish("是");
        announcement.setRead_count("0");
        announcement.setUnread("0");
        announcement.setPublish_time(new Date());
        announcement.setCreator("admin");
        announcement.setStatus(1);
        int i = announcementDao.publishAnnouncement(announcement);
        return i > 0 ? Result.success(announcement) : Result.failure(400,"保存并发布失败");
    }

    @Override
    public Result<Announcement> selectAnnouncementById(int notification_id) {
        Announcement announcement = announcementDao.selectAnnouncementById(notification_id);
        return announcement != null ? Result.success(announcement) : Result.failure(400,"查询失败");
    }

    @Override
    public Result<Announcement> updateAnnouncement(Announcement announcement) {
        int i = announcementDao.updateAnnouncement(announcement);
        return i > 0 ? Result.success(announcement) : Result.failure(400,"修改失败");
    }
}
